package org.framework.lazy.cloud.network.heartbeat.client;

import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.binder.system.FileDescriptorMetrics;
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties;
import org.springframework.boot.actuate.metrics.system.DiskSpaceMetricsBinder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.io.File;
import java.util.List;

@Configuration
@Import({MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class})
public class MetricsConfig {

    // fix cgroup2fs环境
    @Bean
    public UptimeMetrics uptimeMetrics() {
        try {
            return new UptimeMetrics();
        } catch (Exception e) {
            System.err.println("Failed to initialize UptimeMetrics: " + e.getMessage());
            return null;
        }
    }
    @Bean
    public ProcessorMetrics processorMetrics() {
        try {
            return new ProcessorMetrics();
        } catch (Exception e) {
            System.err.println("Failed to initialize ProcessorMetrics: " + e.getMessage());
            return null;
        }
    }
    @Bean
    public FileDescriptorMetrics fileDescriptorMetrics(){
        try {
            return new FileDescriptorMetrics();
        }catch (Exception e){
            System.err.println("Failed to initialize FileDescriptorMetrics: " + e.getMessage());
            return null;
        }
    }
    @Bean
    public DiskSpaceMetricsBinder diskSpaceMetrics(MetricsProperties properties) {
        try {
            List<File> paths = properties.getSystem().getDiskspace().getPaths();
            return new DiskSpaceMetricsBinder(paths, Tags.empty());
        }catch (Exception e){
            System.err.println("Failed to initialize DiskSpaceMetricsBinder: " + e.getMessage());
            return null;
        }
    }
}